package com.lgj.web.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lgj.rocketmq.annotation.EnableMqTransaction;
import com.lgj.rocketmq.code.MqTransaction;
import com.lgj.rocketmq.message.RocketMessage;
import com.lgj.rocketmq.message.RocketMessageBuilder;
import com.lgj.rocketmq.product.RocketMQProducer;
import com.lgj.web.sys.mapper.SysUserMapper;
import com.lgj.web.sys.model.SysUser;
import com.lgj.web.sys.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@EnableMqTransaction
public class TransactionServiceImpl implements TransactionService {
    @Autowired
    private RocketMQProducer rocketMQProducer;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public void test() {
        SysUser user = sysUserMapper.selectByPrimaryKey(1L);
        Message message = RocketMessageBuilder.builder().topic("payment-transaction").body(user).build();
        rocketMQProducer.sendTransactionMessage(message);
    }

    @MqTransaction(topic = "payment-transaction")
    public void localMethod(RocketMessage message) {
        SysUser user = JSONObject.parseObject(message.getBody().toString(),SysUser.class);
        log.info("本地事务处理用户："+ JSONObject.toJSONString(user));
    }

}
